package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.ContractNegDomain;
import top.infopub.mgr.bpa.jsonVo.ContractNegJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ContractNegService;
import top.infopub.mgr.bpa.vo.ContractPublicVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.bpa.BpaContractNeg;
import top.infopub.model.cap.LookInfo;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Tszzzzzz
 * @Date: 2021/6/4
 */

@Controller
@RequestMapping("/contractNeg")
public class ContractNegController extends GenericFlowController {

    @Autowired
    private ContractNegService contractNegService;

    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;
    /**
     * 合同谈判列表数据
     * @return
     */
    @RequestMapping("/queryNegByCondition")
    @ResponseBody
    public RspVo<ContractNegDomain> queryDataByCondition(ContractPublicVo contractPublicVo, ParamPage paramPage) throws ParseException {
        Page<ContractNegDomain> page = new Page<ContractNegDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = contractNegService.selectByDomainWhere(page, contractPublicVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
       BpaContractNeg contractNeg = contractNegService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (contractNeg!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractNeg.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(contractNeg.getProcessId()) && contractNeg.getProcessId().length() > 10){
                request.setAttribute("processId", contractNeg.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(contractNeg.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractNeg.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-ContractNegotiation-detail";
    }

    @RequestMapping("/queryNegDetail")
    @ResponseBody
    public RspVo querySetupDetail(String id) {
        //合同谈判
        ContractNegJsonVo contractNegJsonVo = contractNegService.queryDetailById(id);
        return RspVo.getSuccessResponse(contractNegJsonVo);
    }

   /**
   *@Description:
   *@Param:
   *@return:
   *@Author: Tszzzzzzz
   *@date: 2021/6/6
   */
   @RequestMapping("/save")
   @ResponseBody
   public RspVo save(String bodyStr,Integer submitType){
       AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
       String s = StringEscapeUtils.unescapeJava(bodyStr);
       ContractNegJsonVo vo = JSON.parseObject(s,ContractNegJsonVo.class);
       BpaContractNeg contractNeg = new BpaContractNeg();
       if (StringUtils.isNotBlank(vo.getContractNeg().getId())){ //编辑
           BeanUtils.copyProperties(vo.getContractNeg(),contractNeg);
           contractNeg.setUptBy(userDtl.getId());
           contractNeg.setUptTime(new Date());
           contractNeg.setProCode(vo.getContractNeg().getProCode());
       }else{//新增
           BeanUtils.copyProperties(vo.getContractNeg(),contractNeg);
           contractNeg.setProCode(vo.getContractNeg().getProCode());
           contractNeg.setAddBy(userDtl.getId());
           contractNeg.setAddTime(new Date());
           contractNeg.setUptBy(userDtl.getId());
           contractNeg.setUptTime(new Date());
       }

       /**
        * 附件处理
        * */
       List<ProjectAttachment> attachmentList = new ArrayList<>();
       if(vo.getFileAtt()!=null){
           String[] fileAtt = vo.getFileAtt();
           for (String file : fileAtt) {
               ProjectAttachment attachment = createProjectFileAtta(userDtl,contractNeg.getProCode(),file);
               attachmentList.add(attachment);
           }
       }
       return contractNegService.saveAll(userDtl,contractNeg,attachmentList,bodyStr,submitType);
   }
}
